﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Quarantine
    {
        public bool CreateOneQuarantine(Quarantine_Info objQuarantine_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Quarantine_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"QuarantineID", objQuarantine_Info.QuarantineID));
            command.Parameters.Add(new SqlParameter(@"QuarantineReason", objQuarantine_Info.QuarantineReason));
            command.Parameters.Add(new SqlParameter(@"QuarantineDate", objQuarantine_Info.QuarantineDate));
            command.Parameters.Add(new SqlParameter(@"ReleaseDate", objQuarantine_Info.ReleaseDate));
            command.Parameters.Add(new SqlParameter(@"QuarantinePlaceID", objQuarantine_Info.QuarantinePlaceID));
            command.Parameters.Add(new SqlParameter(@"LivestockID", objQuarantine_Info.LivestockID));
            command.Parameters.Add(new SqlParameter(@"QuarantineStatus", objQuarantine_Info.QuarantineStatus));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneQuarantine(Quarantine_Info objQuarantine_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Quarantine_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@QuarantineID", objQuarantine_Info.QuarantineID));
            command.Parameters.Add(new SqlParameter("@QuarantineReason", objQuarantine_Info.QuarantineReason));
            command.Parameters.Add(new SqlParameter("@QuarantineDate", objQuarantine_Info.QuarantineDate));
            command.Parameters.Add(new SqlParameter("@ReleaseDate", objQuarantine_Info.ReleaseDate));
            command.Parameters.Add(new SqlParameter("@QuarantinePlaceID", objQuarantine_Info.QuarantinePlaceID));
            command.Parameters.Add(new SqlParameter("@LivestockID", objQuarantine_Info.LivestockID));
            command.Parameters.Add(new SqlParameter("@QuarantineStatus", objQuarantine_Info.QuarantineStatus));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneQuarantine(int QuarantineID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Quarantine_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"QuarantineID", QuarantineID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllQuarantine()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Quarantine_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Quarantine_Info GetOneQuarantine(int QuarantineID)
        {
            Quarantine_Info objQuarantine_Info = new Quarantine_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Quarantine_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"QuarantineID", QuarantineID));
            try
            {
                objConnection.Open();
                SqlDataReader ordQuarantine = command.ExecuteReader(); //OjectRecordDatareader
                ordQuarantine.Read();
                if (ordQuarantine.HasRows)
                {
                    if (ordQuarantine["QuarantineID"] != DBNull.Value)
                    {
                        objQuarantine_Info.QuarantineID = Convert.ToInt16(ordQuarantine["QuarantineID"]);
                    }
                    if (ordQuarantine["QuarantineReason"] != DBNull.Value)
                    {
                        objQuarantine_Info.QuarantineReason = Convert.ToString(ordQuarantine["QuarantineReason"]);
                    }
                    if (ordQuarantine["QuarantineDate"] != DBNull.Value)
                    {
                        objQuarantine_Info.QuarantineDate = Convert.ToDateTime(ordQuarantine["QuarantineDate"]);
                    }
                    if (ordQuarantine["ReleaseDate"] != DBNull.Value)
                    {
                        objQuarantine_Info.ReleaseDate = Convert.ToDateTime(ordQuarantine["ReleaseDate"]);
                    }
                    if (ordQuarantine["QuarantinePlaceID"] != DBNull.Value)
                    {
                        objQuarantine_Info.QuarantinePlaceID = Convert.ToInt16(ordQuarantine["QuarantinePlaceID"]);
                    }
                    if (ordQuarantine["LivestockID"] != DBNull.Value)
                    {
                        objQuarantine_Info.LivestockID = Convert.ToInt16(ordQuarantine["LivestockID"]);
                    }
                    if (ordQuarantine["QuarantineStatus"] != DBNull.Value)
                    {
                        objQuarantine_Info.QuarantineStatus = Convert.ToBoolean(ordQuarantine["QuarantineStatus"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objQuarantine_Info;
        }
    }
}
